<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="'库位' + title"
    width="870"
    @close="close"
  >
    <el-form
      ref="formRef"
      inline
      label-width="140px"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="所属仓库：" prop="whid">
        <el-select
          v-model="form.whid"
          placeholder="请选择"
          @change="getAreaData(form.whid)"
        >
          <el-option
            v-for="item in storeList"
            :key="item.WHID"
            :label="item.FullName"
            :value="item.WHID"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.whid" label="所属库区：" prop="whaid">
        <el-select
          v-model="form.whaid"
          placeholder="请选择"
          @change="getShelfData(form.whaid)"
        >
          <el-option
            v-for="item in areaList"
            :key="item.WHAID"
            :label="item.FullName"
            :value="item.WHAID"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="form.whaid" label="所属货架：" prop="whsid">
        <el-select v-model="form.whsid" placeholder="请选择">
          <el-option
            v-for="item in shelfList"
            :key="item.WHSID"
            :label="item.FullName"
            :value="item.WHSID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库位编码：" prop="code">
        <span>{{ codePrefix }}</span>
        <el-input
          v-model="form.code"
          :input-style="{ width: codePrefix ? '130px' : '212px' }"
        />
      </el-form-item>
      <el-form-item label="库位名称：" prop="fullName">
        <el-input v-model="form.fullName" />
      </el-form-item>
      <el-form-item label="库位类型" prop="sType">
        <!-- <el-input v-model="form.sType" /> -->
        <el-select v-model="form.sType" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重量上限" prop="weightLimit">
        <el-input v-model="form.weightLimit" />
      </el-form-item>
      <el-form-item label="体积上限：" prop="volumeLimit">
        <el-input v-model="form.volumeLimit" />
      </el-form-item>
      <el-form-item label="补货触发量：" prop="rtq">
        <el-input v-model="form.rtq" />
      </el-form-item>
      <el-form-item label="物资混放：" prop="itemMixup">
        <el-input v-model="form.itemMixup" />
      </el-form-item>
      <el-form-item label="批次混放：" prop="batchMixup ">
        <el-input v-model="form.batchMixup" />
      </el-form-item>
      <el-form-item label="库位长：" prop="sLength">
        <el-input v-model="form.sLength" />
      </el-form-item>
      <el-form-item label="库位宽：" prop="sWidth">
        <el-input v-model="form.sWidth" />
      </el-form-item>
      <el-form-item label="库位高：" prop="sHeight">
        <el-input v-model="form.sHeight" />
      </el-form-item>
      <el-form-item label="排：" prop="sRow">
        <el-input v-model="form.sRow" />
      </el-form-item>
      <el-form-item label="位：" prop="sPositon">
        <el-input v-model="form.sPositon" />
      </el-form-item>
      <el-form-item label="层：" prop="sLayer">
        <el-input v-model="form.sLayer" />
      </el-form-item>
      <el-form-item label="是否启用：" prop="enabled">
        <el-switch
          v-model="form.enabled"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item label="描述：">
        <el-input
          v-model="form.remark"
          autosize
          placeholder="请输入描述"
          type="textarea"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  // import VabIconSelector from '@/plugins/VabIconSelector'
  import {
    doEdit,
    getwhstorage,
    getareaData,
    getshelfData,
  } from '@/api/storage'
  export default defineComponent({
    name: 'StorageEdit',
    props: {
      areaList: {
        type: Array,
        default: () => [],
      },
      storeList: {
        type: Array,
        default: () => [],
      },
      shelfList: {
        type: Array,
        default: () => [],
      },
    },
    // components: { VabIconSelector },
    emits: ['fetchData'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        params: {},
        queryForm: {
          whid: 'string',
        },
        parentList: [],
        list: [],
        options: [
          {
            value: '备货区',
            label: '备货区',
          },
          {
            value: '拣货区',
            label: '拣货区',
          },
          {
            value: '暂存区',
            label: '暂存区',
          },
          {
            value: '活动区',
            label: '活动区',
          },
          {
            value: '次品区',
            label: '次品区',
          },
        ],
        defaultProps: {
          children: 'ChildNodes',
          label: 'FullName',
        },
        form: {
          meta: {},
        },
        rules: {
          whid: [
            { required: true, trigger: 'change', message: '请选择所属仓库' },
          ],
          whaid: [
            { required: true, trigger: 'change', message: '请选择所属库区' },
          ],
          whsid: [
            { required: true, trigger: 'change', message: '请选择所属货架' },
          ],
          code: [
            { required: true, trigger: 'blur', message: '请输入库区编码' },
          ],
          fullName: [
            { required: true, trigger: 'blur', message: '请输入库区名称' },
          ],
          price: [
            { required: false, message: '请输入转账金额', trigger: 'blur' },
            { pattern: /^(\d+)((?:\.\d+)?)$/, message: '请输入合法金额数字' },
          ],
          area: [
            { required: false, trigger: 'blur', message: '请输入库区面积' },
          ],
          enabled: [
            { required: false, trigger: 'blur', message: '请选择是否可用' },
          ],
          sortCode: [
            { required: false, trigger: 'change', message: '请输入显示顺序' },
          ],
          remark: [
            { required: false, trigger: 'change', message: '请输入描述' },
          ],
          sType: [
            {
              required: true,
              trigger: 'blur,change',
              message: '请选择库区类型',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
        queryTreeForm: {
          pageNo: 1,
          pageSize: 100,
          wheres: `[{"name":"ModuleId","value":"value","displayType":"equal"}]`,
          export: false,
          moduleType: 'ModuleId',
          sort: 'SortCode',
          order: 'ASC',
        },
        areaList: [],
        shelfList: [],
      })
      //获取详情数据
      const getWhstorage = async () => {
        state.listLoading = true
        const {
          data: { data },
        } = await getwhstorage(state.queryForm)
        await getAreaData(data.whid)
        await getShelfData(data.whaid)
        data.WHAID = data.whaid
        state.form = data
        state.listLoading = false
      }
      //编辑传值
      const showEdit = (row, type) => {
        if (type != 'edit') {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.queryForm = { sid: row.SID || row.sid }
          getWhstorage()
        }
        state.dialogFormVisible = true
      }
      //弹框关闭
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          meta: {
            icon: '',
          },
        }
        state.dialogFormVisible = false
      }
      //数据保存
      const save = async () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { message } = await doEdit(state.form)
            $baseMessage(message, 'success', 'vab-hey-message-success')
            close()
            emit('fetchData')
          }
        })
      }
      // 获取库区列表
      const getAreaData = async (WHID) => {
        const params = JSON.parse(JSON.stringify(state.queryTreeForm))
        params.wheres = `[{"name":"WHID","value":${WHID},"displayType":"equal"}]`
        const {
          data: { rows },
        } = await getareaData(params)
        state.areaList = rows
      }
      // 获取货架列表
      const getShelfData = async (WHAID) => {
        const params = JSON.parse(JSON.stringify(state.queryTreeForm))
        params.wheres = `[{"name":"WHAID","value":${WHAID},"displayType":"equal"}]`
        const {
          data: { rows },
        } = await getshelfData(params)
        state.shelfList = rows
      }
      const codePrefix = computed(() => {
        const store = props.storeList.filter((item) => {
          if (item.WHID === state.form.whid) {
            return item
          }
        })[0]?.Code
        const area = state.areaList.filter((item) => {
          if (item.WHAID === state.form.whaid) {
            return item
          }
        })[0]?.Code
        const shelf = state.shelfList.filter((item) => {
          if (item.WHSID === state.form.whsid) {
            return item
          }
        })[0]?.Code
        return `${store ? `${store}-` : ''}${area ? `${area}-` : ''}${
          shelf ? `${shelf}-` : ''
        }`
      })
      return {
        ...toRefs(state),
        showEdit,
        getWhstorage,
        close,
        save,
        getAreaData,
        getShelfData,
        codePrefix,
      }
    },
  })
</script>
<style scoped>
  .el-form {
    display: flex;
    flex-flow: wrap;
    justify-content: left;
  }
  ::v-deep .el-input {
    width: 234px;
  }
  .el-form-item {
    width: 380px;
  }
</style>
